package com.bjsxt.shop.head.springmvc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bjsxt.shop.common.controller.BaseController;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.goods.pojo.AGoods;
import com.bjsxt.shop.goods.pojo.AGoodsCate;
import com.bjsxt.shop.goods.pojo.AGoodsEnum;
import com.bjsxt.shop.goods.service.IGoodsService;

/**
 * 不需要登陆的Controller
 * 
 * @author WangshSxt
 *
 */
@Controller
public class IndexHeadController extends BaseController
{
	@Resource
	private IGoodsService goodsService;
	
	/**
	 * 首页
	 * 
	 * @return
	 */
	@RequestMapping("/index")
	public String index()
	{
		ConstatFinalUtil.SYS_LOGGER.info("==main==");
		return "/head/index";
	}
	
	/**
	 * 商品分类列表
	 * @param cate 分类的url
	 * @return
	 */
	@RequestMapping("/cate_{cateUrl}")
	public String goodsCateList(@PathVariable String cateUrl,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("==goodsCateList==" + cateUrl);
		return this.goodsList(cateUrl, "1", model);
	}
	
	/**
	 * 商品分类列表
	 * @param cate 分类的url
	 * @return
	 */
	@RequestMapping("/goods_{cateUrl}_{currentPage}")
	public String goodsCatePageList(@PathVariable String cateUrl,@PathVariable String currentPage , Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("==goodsCatePageList==" + cateUrl);
		return this.goodsList(cateUrl, currentPage, model);
	}
	
	/**
	 * 重要性:
	 * 为什么要单独抽出一个goodsList方法
	 * 
	 * 不能让url直接访问
	 * @return
	 */
	private String goodsList(String cateUrl,String currentPage,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("==goodsList==" + cateUrl);
		/**
		 * 根据url中的分类的url取出分类的对象
			根据分类的id和启用的状态查商品
		 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("url", cateUrl);
		AGoodsCate goodsCate = this.goodsService.findOneGoodsCateService(condMap);
		
		/* 根据分类的Id和状态启用查询商品 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(currentPage, "16");
		condMap.clear();
		if(goodsCate != null)
		{
			condMap.put("cateId", goodsCate.getId());
		}
		condMap.put("status", AGoodsEnum.STATUS_ENABLE.getStatus());
		Map<String, Object> resultMap = this.goodsService.findCondListGoodsService(pageInfoUtil, condMap);
		List<AGoods> goodsList = (List<AGoods>) resultMap.get("list");
		int totalRecord = Integer.valueOf(resultMap.get("totalRecord") + "");
		pageInfoUtil.setTotalRecord(totalRecord);
		/* 存储结果 */
		model.addAttribute("goodsCate", goodsCate);
		model.addAttribute("goodsList", goodsList);
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		return "/head/goodsList";
	}
}
